package org.example.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Component
@Aspect
public class SysroleAdvice {

    @Pointcut("execution(* org.example.dao.BookDao.*(..))")
    private void sysroleptx(){}

    /**
     *
     * 在方法上添加JoinPoint,通过JoinPoint来获取参数
     *
     * */
//    @Before("sysroleptx()")
    public void mthod2(JoinPoint jp){
        Object[] args = jp.getArgs();
        System.out.println("before advice ..."+ Arrays.toString(args));
    }

//    @After("sysroleptx()")
    public void after(JoinPoint jp) {
        Object[] args = jp.getArgs();
        System.out.println("after advice ..."+ Arrays.toString(args));
    }

//    @Around("sysroleptx()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable{
        Object ret = pjp.proceed();
        Object[] args = pjp.getArgs();
        System.out.println("xx"+Arrays.toString(args));
        return ret;
    }

    //设置返回后通知获取原始方法的返回值，要求returning属性值必须与方法形参名相同
//    @AfterReturning(value = "sysroleptx()",returning = "ret")
    public void afterReturning(JoinPoint jp,String ret) {
        System.out.println("afterReturning advice ..."+ret);
    }

    //设置抛出异常后通知获取原始方法运行时抛出的异常对象，要求throwing属性值必须与方法形参名相同
    @AfterThrowing(value = "sysroleptx()",throwing = "t") //报错后才执行
    public void afterThrowing(Throwable t) {
        System.out.println("afterThrowing advice ..."+t);
    }

}
